package com.test;

import java.nio.ByteBuffer;

/***
 * 缓冲区是NIO 提高给传输文件和通道一起配合使用,存储数据.<br>
 * Buffer<br>
 * ByteBuffer<br>
 * LongBuffer<br>
 * InteigBuffer<br>
 * FloatBuffer<br>
 * DubboBuffer<br>
 *
 */
public class BuffTest1 {

	public static void main(String[] args) {
		try {
			// 初始化byteBuffer大小
			ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
			System.out.println(byteBuffer.position());
			System.out.println(byteBuffer.limit());
			System.out.println(byteBuffer.capacity());
			System.out.println("----------往bytebuff存放数据....----------");
			byteBuffer.put("abcd1".getBytes());
			System.out.println(byteBuffer.position());
			System.out.println(byteBuffer.limit());
			System.out.println(byteBuffer.capacity());
			System.out.println("----------读取值...----------");
			// 开启读取模式
			byteBuffer.flip();
			System.out.println("position:" + byteBuffer.position());
			System.out.println(byteBuffer.limit());
			System.out.println(byteBuffer.capacity());
			byte[] bytes = new byte[byteBuffer.limit()];
			byteBuffer.get(bytes);
			System.out.println(new String(bytes, 0, bytes.length));

			System.out.println("----------重复读取----------");
			byteBuffer.rewind();// 重复读取
			System.out.println("position:" + byteBuffer.position());
			System.out.println(byteBuffer.limit());
			System.out.println(byteBuffer.capacity());
			byte[] bytes2 = new byte[byteBuffer.limit()];
			byteBuffer.get(bytes2);
			System.out.println(new String(bytes2, 0, bytes2.length));
			// 清空缓冲数据被遗忘值...
			System.out.println("----------清空缓冲区--------------");
			byteBuffer.clear();
			System.out.println("position:" + byteBuffer.position());
			System.out.println(byteBuffer.limit());
			System.out.println(byteBuffer.capacity());
			System.out.println((char) byteBuffer.get());

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
